package com.woniuxy.house.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.house.service.BuildingService;
import com.woniuxy.entity.house.Building;
import com.woniuxy.entity.house.dto.BuildingDto;
import com.woniuxy.entity.house.dto.BuildingInfoDto;
import com.woniuxy.entity.house.dto.SelectBuildingDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2021-03-19
 */
@Api(tags = "楼盘接口",description = "管理楼盘的接口")
@RestController
@RequestMapping("/house/building")
public class BuildingController {
    @Resource
    private BuildingService buildingService;

    /**
     *  获取成都的楼盘信息（限制经纬度）
     *  103.932272 < 经度 < 104.20693
     *  30.487946 < 纬度 < 30.783349
     */
    @ApiOperation("获取成都的楼盘信息")
    @GetMapping("/findMapList")
    public ResponseResult<List<Building>> findMapList(){
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("longitude",103.932272,104.20693);
        queryWrapper.between("latitude",30.487946,30.783349);
        queryWrapper.eq("status",0);
        return buildingService.list(queryWrapper) != null ?
                new ResponseResult<>(buildingService.list(queryWrapper)) : new ResponseResult<>(500,"没有房屋信息");
    }

    /**
     * 根据经纬度精确查询楼盘
     */
    @ApiOperation("根据经纬度精确查询楼盘")
    @GetMapping("/findByMap")
    public ResponseResult<BuildingDto> findByMap(BigDecimal longitude, BigDecimal latitude){
        // 通过bid去查询楼盘及户型，再通过户型查询租房及二手房
        return buildingService.findDtoByMap(longitude,latitude) != null ?
                new ResponseResult<>(buildingService.findDtoByMap(longitude,latitude)) : new ResponseResult<>(500,"没有数据");
    }

    /**
     * 楼盘下架
     * @param id
     * @return
     */
    @ApiOperation("楼盘下架")
    @PostMapping("/delete")
    public ResponseResult deleteBuilding(int id){
        if (buildingService.deleteBuilding(id)){
            return ResponseResult.SUCCESS;
        }else{
            return ResponseResult.FAIL;
        }
    }

    /**
     * 楼盘上架
     * @param building
     * @return
     */
    @ApiOperation("楼盘上架")
    @PostMapping("/insert")
    public ResponseResult insertBuilding(Building building){
        if (buildingService.insertBuilding(building)){
            return ResponseResult.SUCCESS;
        }else{
            return ResponseResult.FAIL;
        }
    }

    /**
     * 查询所有楼盘
     * @param current
     * @param size
     * @return
     */
    @ApiOperation("查询所有楼盘")
    @GetMapping("/findAll")
    public ResponseResult<List<BuildingInfoDto>> findBuilding(int current, int size){
          List<BuildingInfoDto> buildingInfoDtoPage = buildingService.findAllBuilding(current, size);
          if(buildingInfoDtoPage.size()>0){
              return new ResponseResult<>(buildingInfoDtoPage);
          }else {
              return new ResponseResult<>(500,"fail");
          }
    }

    /**
     * 条件分页查询
     * @param selectBuildingDto
     * @param current
     * @param size
     * @return
     */
    @ApiOperation("条件分页查询")
    @GetMapping("/findByConditions")
    public ResponseResult<List<BuildingInfoDto>> findByConditions(SelectBuildingDto selectBuildingDto,int current,int size){
        System.out.println(selectBuildingDto);
        List<BuildingInfoDto> buildingInfoDtos = buildingService.findByCondictions(current,size,selectBuildingDto);
        if(buildingInfoDtos.size()>0){
            return new ResponseResult<>(buildingInfoDtos);
        }else {
            return new ResponseResult<>(500, "fail");
        }
    }


    /**
     * 通过地址模糊查询
     * @param address
     * @return
     */
    @ApiOperation("通过地址模糊查询")
    @Deprecated
    @GetMapping("/findByAddress")
    public ResponseResult<Page<Building>> findByAddress(int current,int size,String address){

        QueryWrapper<Building> wrapper = new QueryWrapper<>();
        wrapper.like("building_address",address);
        Page<Building> buildingPage = buildingService.page(new Page(current, size), wrapper);
        return new ResponseResult<>(buildingPage);
    }

    /**
     * 通过价格范围查询楼盘
     * @param min
     * @param max
     * @return
     */
    @ApiOperation("通过价格范围查询楼盘")
    @Deprecated
    @GetMapping("/findByPrice")
    public ResponseResult<Page<Building>> findByAcgPrice(int current,int size,int min,int max){
        QueryWrapper<Building> wrapper = new QueryWrapper<>();
        wrapper.between("avg_price",min,max);
        Page<Building> buildingPage = buildingService.page(new Page<>(current,size),wrapper);
        return new ResponseResult<>(buildingPage);
    }

}

